package com.newcapec.erp.main;

import com.newcapec.erp.bean.AdminEntity;
import com.newcapec.erp.utils.XMLFWriter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateDataUI extends JFrame{
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;

    public UpdateDataUI() {
        setIconImage(Toolkit.getDefaultToolkit().getImage("images/logo.png"));
        setTitle("个人信息");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(467,293);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(10, 22, 425, 27);
        String adminAn="用户账号："+ DataInit.adminEntity.getAdminAn();
        textField.setText(adminAn);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(10, 67, 425, 27);
        String adminName="姓名："+DataInit.adminEntity.getAdminName();
        textField_1.setText(adminName);
        contentPane.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(10, 116, 425, 27);
        String adminTel="手机号码："+DataInit.adminEntity.getAdminTel();
        textField_2.setText(adminTel);
        contentPane.add(textField_2);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(10, 162, 425, 27);
        String adminMail="邮箱："+DataInit.adminEntity.getAdminMail();
        textField_3.setText(adminMail);
        contentPane.add(textField_3);
        textField.setEditable(false);
        textField_1.setEditable(false);
        textField_2.setEditable(false);
        textField_3.setEditable(false);

        JButton button = new JButton("修改信息");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setEditable(true);
                textField_1.setEditable(true);
                textField_2.setEditable(true);
                textField_3.setEditable(true);
            }
        });
        button.setBounds(170, 208, 93, 23);

        contentPane.add(button);
        JButton btnNewButton = new JButton("保存");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //将获取到的字符串进行过滤去除前面的重复部分
                String text = textField.getText();
                String ExadminAn = text.replace("用户账号：", "");
                String text1 = textField_1.getText();
                String ExadminName = text1.replace("姓名：", "");
                String text2 = textField_2.getText();
                String ExadminTel = text2.replace("手机号码：", "");
                String text3 = textField_3.getText();
                String ExadminMail = text3.replace("邮箱：", "");
                AdminEntity exadminEntity=new AdminEntity(ExadminAn,null,ExadminName,ExadminTel,ExadminMail);
                XMLFWriter.ChangeAdmin(exadminEntity);
                JOptionPane.showMessageDialog(null, "恭喜你，修改成功.", "提示",JOptionPane.PLAIN_MESSAGE);

            }
        });
        btnNewButton.setBounds(280, 208, 93, 23);
        contentPane.add(btnNewButton);
        setLocationRelativeTo(null);
        setVisible(true);
    }
}
